<?php

namespace frontend\controllers;

use backend\models\BuildCity;
use backend\models\BuildType;
use frontend\models\GrpCity;
use frontend\models\UserFavourite;
use Yii;

class MyHomeController extends \yii\web\Controller
{
    public function actionIndex()
    {
        if (Yii::$app->getUser()->isGuest) {
            return $this->redirect(['site/login']);
        }
        $id = Yii::$app->request->post('id');
        $uid = Yii::$app->getUser()->getId();

        $city = GrpCity::find()
            ->where(['not', ['search_index' => null]])
            ->orderBy(['search_index' => SORT_ASC])
            ->asArray()
            ->all();
        $city = $this::dealDate($city);
        $favo = UserFavourite::find()
            ->where(['uid' => $uid, 'type' => 3])
            ->with(['buildType.buildCity.cityName'])
            ->asArray()
            ->all();
        return $this->render('index', [
            'city' => $city,
            'favo' => $favo,
        ]);
    }

    private function dealDate($data)
    {
        $res = array();
        foreach ($data as $k =>$v) {
            $res[$v['search_index']][] = $v;
        }
        return $res;
    }

    //获取楼盘列表
    public function actionGetHourse()
    {
        if (Yii::$app->request->isGet) {
            $id = Yii::$app->request->get('id');

            $buildCity = BuildCity::find()
                ->where(['city_id' => $id])
                ->andWhere(['not', ['search_index' => null]])
                ->orderBy(['search_index' => SORT_ASC])
                ->asArray()
                ->all();
            $buildCity = $this::dealDate($buildCity);
            return $this->render('hourse', [
                'buildCity' => $buildCity,
            ]);
        }
    }
    //楼盘信息
    public function actionHourseMsg()
    {
        if (Yii::$app->request->isGet) {
            $bid = Yii::$app->request->get('id');

            $buildType = BuildType::find()
                ->with(['buildCity', 'imgL', 'favo'])
                ->where(['bid' => $bid])
                ->asArray()
                ->all();
            return $this->render('hourseImg',[
                'buildType' => $buildType,
            ]);
        }
    }

    //收藏户型
    public function actionCollectionHourse()
    {
        $ret = array();

        if (Yii::$app->getUser()->isGuest) {
            $ret['status'] = -1;
            $ret['msg'] = '请先登录';
            return json_encode($ret, JSON_UNESCAPED_UNICODE);
        }
        if (Yii::$app->request->isPost) {
            $id = Yii::$app->request->post('id');
            $uid = Yii::$app->getUser()->getId();

            $favo = UserFavourite::find()->where(['type_id' => $id, 'uid' => $uid]);
            if ($favo->exists()) {
                UserFavourite::deleteAll(['type_id' => $id, 'uid' => $uid]);
                $ret['status'] = 2;
                $ret['msg'] = '已取消收藏';
                return json_encode($ret, JSON_UNESCAPED_UNICODE);
            } else {
                $addFavo = new UserFavourite();
                $addFavo->uid = $uid;
                $addFavo->type = 3;
                $addFavo->type_id = $id;
                $addFavo->addtime = time();
                if ($addFavo->save()) {
                    $ret['status'] = 1;
                    $ret['msg'] = '收藏成功';
                    return json_encode($ret, JSON_UNESCAPED_UNICODE);
                }
            }
        }
    }
}
